$(document).ready(function(){
	
	$('#putBillNumber').focus();
	
	$("#putBillNumber").keypress(function (event) {
		if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
	       return false;
	    }
	});
});

//เมื่อกดปุ่ม Enter
function Enter(e) {
	if (e.keyCode == 13) {
		searchAccount();
	  return false;  
	}
};

//Function ยืนยันการยกเลิกข้อมูลใบแจ้งหนี้
function confirm(){
	var billNumber = $("#billNumber").text();
	var arReason = $("#arReason").val();
	if (billNumber == "") {
		$('#checkResultModal').modal();
	} else if (arReason == "") {
		$('#checkReasonModal').modal();
	} else {
		$('#confirmCancelModal').modal();
	}
}

//Function บันทึกการยกเลิกข้อมูลใบแจ้งหนี้
function save(){
	var billNumber = $("#billNumber").text();
	var reason = $("#arReason").val();
	$.ajax({
		url : "CS42003Srvl",
		type : "POST",
		data : {methodType: "saveData", 
			billNumber:billNumber,
			reason:reason,
			url_address:$("#url_address").val(),
			agent_browser:$("#agent_browser").val()	
		},
		async : false ,
		success : function(data) {
			var newdata = jQuery.parseJSON(data);
			if (newdata.message == "success") {
				$('#confirmCancelModal').modal('hide');
				$('#successModal').modal();
				$('#arReason').val("");
				$('#putBillNumber').val("");
			} else {
				$('#confirmCancelModal').modal('hide');
				$('#failModal').modal();
			}
			
		} 
	});
}

//Function ค้นหา เลขที่ใบแจ้งหนี้
function searchAccount() {
	var billNumber = $('#putBillNumber').val();
	if (billNumber == "") {
		$('#failAccountModal').modal();
	} else {
		$.ajax({           	
    		url : "CS42003Srvl",
    		type : "POST",
    		data : {methodType:"getDataAR",
    			billNumber:billNumber,
    			url_address:$("#url_address").val(),
    			agent_browser:$("#agent_browser").val()
    			},
    		async : false ,
    		success : function(data) {
    			var newdata = jQuery.parseJSON(data);
    			if (newdata.message == "resultNull"){
    				$('#messageBoxSearch').text("ไม่พบข้อมูลใบแจ้งหนี้นี้");
    				$('#failResultModal').modal();
    			} else if (newdata.message == "status") {
    				$('#messageBoxSearch').text("ไม่พบข้อมูลใบแจ้งหนี้นี้ เนื่องจากใบแจ้งหนี้ได้ถูกยกเลิกไปแล้ว");
    				$('#failResultModal').modal();
    			} else if (newdata.message == "receiveDate") {
    				$('#messageBoxSearch').text("ไม่พบข้อมูลใบแจ้งหนี้นี้ เนื่องจากใบแจ้งหนี้มีการรับเงินไปแล้ว");
    				$('#failResultModal').modal();
    			} else {
        			$("#account").text(newdata.account);
        			$("#zoneName").text(newdata.zoneName);
        			$("#routeDescription").text(newdata.routeDescription);
        			$("#sequence").text(newdata.sequence);
        			$("#fullName").text(newdata.fullName);
        			$("#customerClass").text(newdata.customerClass);
        			$("#address").text(newdata.address);
        			$("#billNumber").text(newdata.billNumber);
        			$("#accountReceivableData").text(newdata.accountReceivableData);
        			$("#consumption").text(newdata.consumption);
        			$("#waterCharge").text(newdata.waterCharge);
        			$("#waterFee").text(newdata.waterFee);
        			$("#vatAmount").text(newdata.vatAmount);
        			$("#billAmount").text(newdata.billAmount);
        			$("#garbageCharge").text(newdata.garbageCharge);
        			$("#wasteWaterCharge").text(newdata.wasteWaterCharge);
        			$("#otherCharge").text(newdata.otherCharge);
        			$("#totalAmount").text(newdata.totalAmount);
        			$("#arReason").val("");
        			$("#arReason").focus();
    			}
    		}
		});	
	}
}

//Function เคลียร์หน้าจอ
function clearScreen(){
	$("#account").text("");
	$("#zoneName").text("");
	$("#address").text("");
	$("#routeDescription").text("");
	$("#sequence").text("");
	$("#fullName").text("");
	$("#customerClass").text("");
	$("#address").text("");
	$("#billNumber").text("");
	$("#accountReceivableData").text("");
	$("#consumption").text("0");
	$("#waterCharge").text("0.00");
	$("#waterFee").text("0.00");
	$("#vatAmount").text("0.00");
	$("#billAmount").text("0.00");
	$("#garbageCharge").text("0.00");
	$("#wasteWaterCharge").text("0.00");
	$("#otherCharge").text("0.00");
	$("#totalAmount").text("0.00");
	$("#arReason").val("");
}

//รูปแบบ ตัวเลข
function format(num, fix) {
    var p = num.toFixed(fix).split(".");
    return p[0].split("").reduceRight(function(acc, num, i, orig) {
        if ("-" === num && 0 === i) {
            return num + acc;
        }
        var pos = orig.length - i - 1
        return  num + (pos && !(pos % 3) ? "," : "") + acc;
    }, "") + (p[1] ? "." + p[1] : "");
}

//Function ปิด Popup ค้นหา
function getFocus(){
	$(".close").trigger('click');
	$('#putBillNumber').focus();
	clearScreen();
}

// Funtion ปิด Popup ตรวจสอบเหตุผล
function getFocus2(){
	$(".close").trigger('click');
	$('#arReason').focus();
}

//Function ปิด Popup ยืนยัน
function cancel(){
	$(".close").trigger('click');
	$('#arReason').focus();
}